package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Prod;
import com.powernode.domain.Sku;
import com.powernode.model.StockChange;
import com.powernode.service.ProdService;
import com.powernode.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@RequestMapping("prod/prod")
@Api(tags = "产品管理")
public class ProdController {

    @Autowired
    private ProdService prodService;

    @Autowired
    private SkuService skuService;


    @GetMapping("page")
    @ApiOperation("分页查询商品列表")
    @PreAuthorize("hasAuthority('prod:prod:page')")
    public ResponseEntity<Page<Prod>> loadProdPage(Page<Prod> page, Prod prod) {
        Page<Prod> prodPage = prodService.selectProdPage(page, prod);
        return ResponseEntity.ok(prodPage);
    }


    @PostMapping
    @ApiOperation("新增商品")
    @PreAuthorize("hasAuthority('prod:prod:save')")
    public ResponseEntity<Void> addProd(@RequestBody Prod prod) {
        prodService.saveProd(prod);
        return ResponseEntity.ok().build();

    }


    ///////////////////远程调用的接口  远程调用的接口 一般不设置权限/////////////////

    /**
     * 远程调用查询商品的信息
     *
     * @param prodId
     * @return
     */
    @GetMapping("getProdInfoByProdId")
    @ApiOperation("提供远程调用查询商品的信息")
    Prod getProdInfoByProdId(@RequestParam Long prodId) {
        return prodService.getById(prodId);
    }

    @GetMapping("getSkuListByIds")
    @ApiOperation("提供远程调用根据skuIds查询sku集合")
    List<Sku> getSkuListByIds(@RequestParam List<Long> skuIds) {
        return skuService.listByIds(skuIds);
    }
    @PostMapping("changeStock")
    @ApiOperation("修改prod和sku的库存")
    Boolean changeStock(@RequestBody StockChange stockChange) {
        return prodService.changeStock(stockChange);
    }

    @GetMapping("prod/prodInfo")
    @ApiOperation("根据prodId查询prod信息和sku信息")
    public ResponseEntity<Prod> getProdAndSkuByProdId(Long prodId) {
        Prod prod = prodService.selectProdAndSkuByProdId(prodId);
        return ResponseEntity.ok(prod);
    }
}
